'use strict'
import { defineConfig } from 'vite'
import legacy from '@vitejs/plugin-legacy'
import { createVuePlugin } from 'vite-plugin-vue2'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'

const { resolve } = require('path')

export default defineConfig({
  base: '/admin/',
  publicDir: 'public',
  server: {
    proxy: {
      '/api/admin': {
        target: 'https://XXx',
        changeOrigin: true
      }
    },
    port: 7080,
    host: '0.0.0.0'
  },
  // vite移除了process，需要单独定义
  define: {
    'process.env': {}
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      '@': resolve(__dirname, 'src'),
      '~@': resolve(__dirname, 'src')
    }
  },
  css: {
    preprocessorOptions: {
      sass: {
        additionalData: `$injectedColor: orange;`
      }
    }
  },
  plugins: [
    createVuePlugin({
      jsx: true,
      jsxOptions: {
        injectH: false
      }
    }),
    // 兼容ie
    legacy({
      targets: ['ie >= 11'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    }),
    // svg 图标
    createSvgIconsPlugin({
      iconDirs: [resolve(process.cwd(), 'src/icons')], // 在src下新建文件夹icons
      symbolId: 'icon-[dir]-[name]'
    }),
    // mock配置
    viteMockServe({
      // 解析，路径可根据实际变动
      mockPath: 'mock',
      // 此处可以手动设置为true
      localEnabled: true,
      prodEnabled: false,
      // 监听文件内容变更
      watchFiles: true
    })
  ]
})
